(function( $ ){

  $.fn.getResults = function( start ) {
    $('#spinner').spin("large", "white");
    var text = $('.form-container .search-field').val();
    $.getJSON("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q="+encodeURI("filetype:jnlp "+text)+"&start="+start+"&callback=?", function(json) { 
        if(json.responseData != null) {
            for(var i in json.responseData.results) {
                $.fn.parseJnlp(json.responseData.results[i].url);
            }
        } else {
            $("#results").html('<td>google search service not responding</td>');
            $('#spinner').spin(false);
        }
    }).error(function() {
        $("#results").html('<td>google search service not responding</td>');
        $('#spinner').spin(false);
    });
    
  };
  $.fn.parseJnlp = function( link ) {
      var query = "select * from xml where url = '" + link + "'";
      $.getJSON("http://query.yahooapis.com/v1/public/yql?q="+encodeURI(query)+"&format=json", function(json) { 
          if(json.query.results != null) {
              var title = json.query.results.jnlp.information.title;
              var image = "";
              if(json.query.results.jnlp.information.icon != null) {
                  if(json.query.results.jnlp.information.icon.href != null) {
                      image = json.query.results.jnlp.information.icon.href;
                  } else {
                      image = json.query.results.jnlp.information.icon[0].href;
                  }
              }
              if(image !=null && image.indexOf('://')==-1 && image != '') {
                  var codebase = json.query.results.jnlp.codebase;
                  image = codebase + (('/' + image).replace('//','/'));
              }
              var result = $.fn.getFormat(link,image,title);
              var temp = $('#results').html();
              $('#results').html(temp + result);
          } else {
            var temp = $('#results').html();
            $("#results").html(temp + $.fn.getDefaultFormat(link));
          }
          $('#spinner').spin(false);
      }).error(function() {
          var temp = $('#results').html();
          $("#results").html(temp + $.fn.getDefaultFormat(link));
          $('#spinner').spin(false);
      });
  };
  $.fn.getDefaultFormat = function( link ) {
	var title = link.substring(link.lastIndexOf('/')+1).replace('.jnlp','');
        return $.fn.getFormat(link,'',title);
  };
  $.fn.getFormat = function(link,image,title) {
        var result = '<td width=126 height=160><a href="{0}"><img src="{1}" width="125" height="125"/><br>{2}</a></td></tr></table>';
        result = result.replace('{0}',link);
        result = result.replace('{1}',image);
        result = result.replace('{2}',title);
        result = result.replace('<img src="" width="125" height="125"/>','<img src="img/java.png" width="125" height="125"/>');
        return result;
  };
  $.fn.searchAction = function( start ) {
	if(!$('.form-container .search-field').val() || $('.form-container .search-field').val() == "Type search text here...") {
		$('.popup').css({ opacity: 0 });
		$('.popup').animate(
			{ opacity: 1 },
			{
				duration: 'slow',
				easing: 'easeOutBounce'
			});
		return false;
	} else {
                $('#search-left').css({ opacity: 1 });
                $('#search-right').css({ opacity: 1 });
	}
	$('#results').html('');
	$.fn.getResults(start);
  };
})( jQuery );

$(document).ready(function() {

        $.myvariable = 4;

        $('#results').html('<td><img src="img/java.png" width=200 height=200/></td>');

        $('#search-left').css({ opacity: 0 });

        $('#search-right').css({ opacity: 0 });

	$('.popup').css({ opacity: 0 });

	$('.submit').click(function() {
		$.fn.searchAction($.myvariable);
	});

        $('#search-left').click(function() {
		if($.myvariable !=4) {
			$.myvariable -=4;
                }
		$.fn.searchAction($.myvariable);
        });

        $('#search-right').click(function() {
		$.myvariable +=4;
		$.fn.searchAction($.myvariable);
        });

	$('.form-container .search-field').focus(function() {
		if($(this).val() == "Type search text here...") {
			this.value = "";
		}
	});

	$('.form-container .search-field').keydown(function() {
		$('.popup').css({ opacity: 0 });
	});

});